package cn.jiahua.hospital.service.impl;

import cn.jiahua.hospital.dto.PatientsByOrderDto;
import cn.jiahua.hospital.mapper.ReportMapper;
import cn.jiahua.hospital.pojo.Report;
import cn.jiahua.hospital.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.constants.Constants;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Report getReportById(Integer id) {
        return reportMapper.getReportById(id);
    }

    @Override
    public List<Report> getReportListByMap(Map<String, Object> param) {
        return reportMapper.getReportListByMap(param);
    }

    @Override
    public Integer getReportCountByMap(Map<String, Object> param) {
        return reportMapper.getReportCountByMap(param);
    }

    @Override
    public Integer qdtxAddReport(Report report) {
        report.setCreateTime(new Date());
        return reportMapper.insertReport(report);
    }

    @Override
    public Integer qdtxModifyReport(Report report) {
        return reportMapper.updateReport(report);
    }

    @Override
    public Integer qdtxDeleteReportById(Integer id) {
        return reportMapper.deleteReportById(id);
    }

    @Override
    public Integer qdtxBatchDeleteReport(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return reportMapper.batchDeleteReport(param);
    }

    @Override
    public Page<Report> queryReportPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = reportMapper.getReportCountByMap(param);
        pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Report> reportList = reportMapper.getReportListByMap(param);
        page.setList(reportList);
        return page;
    }

    @Override
    public PatientsByOrderDto getReportByOrder(String orderId) {
        PatientsByOrderDto reportByOrder = reportMapper.getReportByOrder(orderId);
        return reportByOrder;
    }
}